//
// Created by lz on 3/16/21.
//

#ifndef MYWEBLIB_NET_HTTPCONTEXT_H_
#define MYWEBLIB_NET_HTTPCONTEXT_H_

#include "base/copyable.h"
#include "net/http/HttpRequest.h"

namespace lzweb
{
	namespace net
	{
		class Buffer;


		class HttpContext: public lzweb::copyable
		{
		 public:
			enum HttpRequestParseState
			{
				kExpectRequestLine,
				kExpectHeaders,
				kExpectBody,
				kGotAll,
			};

			HttpContext()
				: state_(kExpectRequestLine)
			{
			}
			// default copy-ctor, dtor and assignment are fine

			// return false if any error
			bool parseRequest(Buffer* buf, TimeStamp receiveTime);

			bool gotAll() const
			{ return state_ == kGotAll; }

			void reset()
			{
				state_ = kExpectRequestLine;
				HttpRequest dummy;	//dummy->傀儡
				request_.swap(dummy);
			}

			const HttpRequest& request() const
			{
				return request_;
			}
			HttpRequest& request()
			{
				return request_;
			}

		 private:
			bool processRequestLine(const char* begin, const char* end);

			HttpRequestParseState state_;  //状态 用于有限状态机
			HttpRequest request_;	//请求
		};
	} //namespace net
}//namespace lzweb



#endif //MYWEBLIB_NET_HTTPCONTEXT_H_
